mcmodhelpfandomcom-20200213-history
Tutorials: The Mod Class
The Mod Class is the meat and potatoes of the mod. Everything is brought together here to make the mod run. How does this happen? Well, we are going to create a few methods to start the initialization process. We are also going to create a Reference class that sets the mod ID, the mod name, and the version as well as some proxies later on. Environment Overview Before we do any coding, we need to get acquainted with the Eclipse interface. # The Programming Window is where the magic happens. It's where you enter your code and make your program. # The Package Explorer is a top-down file tree of all the packages, classes, files, and sub-packages within. It is a good idea to set the view of the explorer to Hierarchical before you start, otherwise it will be difficult to navigate in your project. To do this, find the white drop down arrow next the Package Explorer tab called View Menu. Click it then go to Package Presentation > Hierarchical. # The Diagnostics Window is where the console (outputs text), error log, Javadocs, and declarations are documented and updated. If you get an error in your code, Eclipse with call an exception and print it to the console. You can then remedy this exception and get your code working properly. This is a very useful tool and will and should be used on a regular basis. # The Outline Window shows a hierarchical view of the individual methods, constructors, and variables within the class you are currently viewing. # The Task Window shows the TO-DO comments in all of the classes. This can be useful, but we are not going to use it while making our mod so don't worry about it for now. Setting up Your Project Now that you are aware of how the interface works, lets make the project. First, navigate to the Minecraft folder in the Package Explorer, open it and open the src/main/java package. The package usually contains an example mod which you can open and look through to get a rough idea of how a mod works. Go ahead and delete this class so that the package is empty. Select the empty package, right click on it, and go to New > Package. A dialog should open with a field called Source folder and another with the Name of your package. This is where you name your new package to store your entire mod; you can do this in anyway you want, but make sure that you use periods instead of slashes. For example, I'm going to create a package called com.flava.wiki but you can replace 'tutorial' with the name of your mod. Once you have created the package, select it, right click on it and select New > Class. There are several options in this window, but all we need to worry about is the Name field where you will enter the name of your mod. I'm going to call mine WikiMod, but again you can call it whatever you like. The Initialization Methods Now you should be in an empty class with just the package and class declarations. We are now going to create the PreInit, Init, and PostInit methods. These methods control what happens before, during, and after the mod has started respectively. Once you create these methods, they will throw errors. That is because we have not imported them yet. To import them, press Ctrl + Shift + O. This will automatically import any objects that are not imported yet. Save all classes by pressing Ctrl + Shift + S. @EventHandler public void preInit(FMLPreInitializationEvent event) { } @EventHandler public void init(FMLInitializationEvent event) { } @EventHandler public void postInit(FMLPostInitializationEvent event) { } You may be wondering what the @EventHandler annotation means. They tell Eclipse that the parameter 'event' is, indeed, an event and should be treated like one since there are many other parameter types that they could be mistaken for. The Reference Class To make things easier, we are going to create a reference class that has information such as the mod ID (useful in texturing and if you have multiple mods), the mod name, and the mod version. Later we will add proxies to the class, but for now we are going to create it. # Go to your com.name.mod package and create a new class in it called Reference. # Inside the class create three final Strings as follows: public static final String MOD_ID = ""; public static final String MOD_NAME = ""; public static final String VERSION = "1.0"; The mod ID should be an abbreviation or a short phrase that you can call on easily. It will set this mod apart from any other mods you make, if you do make others. The version is, obviously, the current version of your mod, so it would be a good idea to update it if you change a lot after distribution. Now go back into your mod class and add the follow line of code above your 'public class' declaration: @Mod(modid = Reference.MOD_ID, name = Reference.MOD_NAME, version = Reference.VERSION) public class YourMod { Go ahead and import the @Mod annotation and bingo bango you now have a Mod Class!